import Request from "@/api/request";
import { constObjs } from "@/const/const";
import { SystemSetting } from "@/interface/entity";
import { TablePattern } from "@/interface/general";
import store from "@/store";

export async function updateSystemSetting(setting: SystemSetting) {
  Request({
    method: "put",
    url: "SystemSetting/UpdateSystemSetting",
    data: JSON.stringify(setting)
  });
}

export async function querySystemSetting(guid = "123") {
  Request({
    method: "get",
    url: "SystemSetting/QuerySystemSetting",
    params: {
      guid: guid
    }
  }).then(response => {
    const setting = response.data as SystemSetting;
    constObjs.projectsTablePattern = JSON.parse(
      setting.projectTablePattern
    ) as TablePattern[];
    constObjs.incomeStreamTablePattern = JSON.parse(
      setting.incomeStreamTablePattern
    ) as TablePattern[];
    constObjs.outlayStreamTablePattern = JSON.parse(
      setting.outlayStreamTablePattern
    ) as TablePattern[];
    constObjs.userPermissionTablePattern = JSON.parse(
      setting.adminTablePattern
    ) as TablePattern[];
    store.state.systemSetting = setting;
  });
}
